package cn.yitun.chat.yitunservercommonlib.crud.yitun;

import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrudServiceImpl
        <D extends CrudDao<DTO , PO , VO>,
                DTO extends BaseDto, PO extends BaseInfo, VO >
        extends BaseServiceImpl {
    /**
     * 持久层对象
     */
    @Autowired
    protected D dao;

    /**
     * 获取数量
     * @param params
     * @return
     */
    public int count(Map<String, Object> params) {
        return dao.count(params);
    }


    // -------- ***PO*** ---------
    public PO get(Long id) {
        return dao.get(id);
    }

    public List<PO> list(Map<String, Object> params) {
        return dao.list(params);
    }

    public List<PO> list() {
        return dao.list(new HashMap<>());
    }

    public List<PO> list( PO entity ) {
        return dao.list(YitunMapUtil.java2Map(entity ,false));
    }


    // -------- ***VO*** ---------
    public VO getVO(Long id) {
        return dao.getVO(id);
    }

    public List<VO> listVO(Map<String, Object> params) {
        return dao.listVO(params);
    }

    public List<VO> listVO() {
        return dao.listVO(new HashMap<>());
    }

    public List<VO> listVO(DTO entity) {
        return dao.listVO(YitunMapUtil.java2Map(entity ,false));
    }

    @Transactional
    public int remove(Long id) {
        return dao.remove(id);
    }


    @Transactional
    public int batchRemove(Long[] ids) {
        return dao.batchRemove(ids);
    }

    @Transactional
    public int batchRemove(List<Long> ids) {
        return dao.batchRemoveList(ids);
    }

    // -------- ***DTO*** ---------
    /**
     * 插入
     * @param entity
     */
    @Transactional
    public int save(DTO entity) {
        return dao.save(entity);
    }
    /**
     * 更新
     * @param entity
     */
    @Transactional
    public int update(DTO entity) {
        return dao.update(entity);
    }


}
